﻿using FluentScheduler;

namespace Bigway.Common.Jobs;

public class BackgroundJobInfo
{
    private readonly object _locker = new();
    public required IBackgroundJob Job { get; set; }
    public required Schedule Schedule { get; set; }

    public async Task ScheduleToRunImmediatelyWithDelayAsync(TimeSpan delay, CancellationToken token = default)
    {
        Job.Wait();
        await Task.Delay(delay, token);
        lock (_locker)
        {
            Schedule.Execute();
        }
    }

    public Task ScheduleToRunImmediatelyWithDefaultDelayAsync(CancellationToken token = default)
    {
        return ScheduleToRunImmediatelyWithDelayAsync(CommonStatics.BackgroundJobWaitDefaultDelay, token);
    }

    public void ScheduleToRunImmediately()
    {
        Job.Wait();

        lock (_locker)
        {
            Schedule.Execute();
        }
    }
}